interface ILyric {
  time: number;
  text: string;
}

const timeReg = /^\[(\d{2}):(\d{2})\.(\d{2,3})\]/;
const ParseLyric = (lyric: string) => {
  const newLyric: ILyric[] = [];
  const lineStrings: string[] = lyric.split("\n");
  for (const item of lineStrings) {
    const result = timeReg.exec(item);
    if (!result) continue;

    const m = Number(result[1]) * 60 * 1000;
    const s = Number(result[2]) * 1000;
    const ms = result[3].length === 3 ? Number(result[3]) : Number(result[3]) * 10;
    const time = m + s + ms;
    const text = result?.input.replace(result[0], "");
    newLyric.push({ time, text });
  }
  return newLyric;
};

export default ParseLyric;
